package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * Author: 笨蛋超人不会飞
 * Date:  18:07
 */

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理")
@Slf4j
public class DishController {
    @Autowired
    DishService dishService;

    @Autowired
    RedisTemplate redisTemplate;


    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

    //新增菜品
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){
        //日志输出
        log.info("菜品信息：{}",dishDTO);

        //调用service层方法
        dishService.save(dishDTO);
        String key="dish_"+dishDTO.getCategoryId();
        cleanCache(key);
        return Result.success();

    }

    //分页查询菜品，可以根据name，categoryid,status，page，pageSize进行组合查询
    //status：0 停售 1 起售
    //用DishPageQueryDTO对象接收
    //返回PageResult对象
    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        //调用service层方法调用service层方法
        PageResult dishPageResult = dishService.getAllDish(dishPageQueryDTO);
        //返回
        return Result.success(dishPageResult);
    }

    //批量删除菜品
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        //调用service层方法
        dishService.deleteByIds(ids);
//                清理缓存数据
        cleanCache("dish_*");
        return Result.success();
    }

    //根据id查询菜品信息
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id){
        //调用service层方法
        DishVO dishVO = dishService.getDishById(id);
        //返回
        return Result.success(dishVO);
    }

    //根据id修改菜品信息
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO) {
        //日志输出
        log.info("菜品信息：{}", dishDTO);

        //调用service层方法
        dishService.update(dishDTO);
        //清理缓存数据
        cleanCache("dish_*");
        return Result.success();
    }

    //根据id起售或停售菜品
    @PostMapping("/status/{status}")
    public Result changeStatus(@PathVariable Integer status, @RequestParam List<Long> id){
        //调用service层方法
        dishService.changeStatus(status,id);
        //清理缓存数据
        cleanCache("dish_*");
        return Result.success();
    }

    /*
    * 根据分类的id查询相应的菜品
    * */
    @RequestMapping("/list")
    public  Result<List<Dish>> list(@RequestParam Long categoryId){
        //日志输出
        log.info("根据分类的id查询相应的菜品，分类id：{}",categoryId);
        //调用service层方法
        List<Dish> dishList = dishService.list(categoryId);
        //返回结果
        return Result.success(dishList);



    }

}
